<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二手书交易平台 - 注册</title>
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
            font-family: 'Roboto', sans-serif;
            min-height: 100vh;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .register-container {
            background: #fff;
            padding: 40px 32px 32px 32px;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.15);
            width: 370px;
            max-width: 95vw;
            text-align: center;
        }
        .register-title {
            font-size: 2rem;
            font-weight: 700;
            color: #2d3a4b;
            margin-bottom: 16px;
        }
        .register-subtitle {
            color: #6c7a89;
            font-size: 1rem;
            margin-bottom: 32px;
        }
        .register-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .register-form input {
            padding: 12px 14px;
            border: 1px solid #d1d9e6;
            border-radius: 8px;
            font-size: 1rem;
            outline: none;
            transition: border 0.2s;
        }
        .register-form input:focus {
            border-color: #7eb6ff;
        }
        .register-btn {
            background: linear-gradient(90deg, #7eb6ff 0%, #5eaefd 100%);
            color: #fff;
            font-weight: 700;
            border: none;
            border-radius: 8px;
            padding: 12px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: background 0.2s;
        }
        .register-btn:hover {
            background: linear-gradient(90deg, #5eaefd 0%, #7eb6ff 100%);
        }
        .register-footer {
            margin-top: 24px;
            color: #a0aab8;
            font-size: 0.95rem;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-title">二手书交易平台</div>
        <div class="register-subtitle">注册新账号，加入书籍循环社区</div>
        <form class="register-form" id="registerForm">
            <input type="text" id="username" name="username" placeholder="用户名" required>
            <input type="text" id="phone" name="phone" placeholder="手机号" required>
            <input type="email" id="email" name="email" placeholder="邮箱" required>
            <input type="password" id="password" name="password" placeholder="密码" required>
            <input type="password" id="confirmPassword" name="confirmPassword" placeholder="确认密码" required>
            <div style="text-align:left;font-size:0.98rem;">
                <span style="margin-right:12px;">身份：</span>
                <label style="margin-right:18px;"><input type="radio" name="userType" value="0" checked> 用户</label>
                <label><input type="radio" name="userType" value="1"> 商家</label>
            </div>
            <button type="submit" class="register-btn">注册</button>
        </form>
        <div class="register-footer">已有账号？<a href="login.html" style="color:#5eaefd;text-decoration:none;">立即登录</a></div>
    </div>
    <script>
        document.getElementById('registerForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            var pwd = document.getElementById('password').value;
            var cpwd = document.getElementById('confirmPassword').value;
            if (pwd !== cpwd) {
                alert('两次输入的密码不一致！');
                return;
            }
            var username = document.getElementById('username').value.trim();
            var phone = document.getElementById('phone').value.trim();
            var email = document.getElementById('email').value.trim();
            var userType = document.querySelector('input[name="userType"]:checked').value;
            // 简单校验
            if (!username || !phone || !email || !pwd) {
                alert('请填写所有必填项');
                return;
            }
            try {
                const response = await fetch('./api_config.js');
                const { API_BASE_URL } = await import('./api_config.js');
                const res = await fetch(`${API_BASE_URL}/userAccount`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        username,
                        password: pwd,
                        phone,
                        email,
                        userType: parseInt(userType),
                        status: 1,
                        nickname: ''
                    })
                });
                const result = await res.json();
                if (result.success) {
                    alert('注册成功！请登录');
                    window.location.href = 'login.html';
                } else {
                    alert(result.message || '注册失败');
                }
            } catch (err) {
                alert('网络错误或服务器无响应');
            }
        });
    </script>
</body>
</html> 